package com.gmail.zstate.rtspcam;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class VideoCaptureActivity extends Activity implements SurfaceHolder.Callback {

	private Camera mCamera;
	
	private MediaRecorder mRecorder;
	
	private SurfaceView mPreview;
	
	private Button mRecordButton;
	
	private boolean mRecording = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mRecordButton = (Button) findViewById(R.id.button_record);
		mRecordButton.setText("Start Recordind");
		
		mPreview = (SurfaceView) findViewById(R.id.surface_video);
		mPreview.getHolder().addCallback(this);
		mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		mRecorder = new MediaRecorder();
	}
	
	@Override
    protected void onStop() {
        super.onStop();
    }
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
        finish();
	}

	
	public void onRecordClick(View v) {
		updateRecordingState();
	}

	private void updateRecordingState() {
		if(mRecording) {
			mRecording = false;
			mRecorder.stop();
			mRecorder.reset();
			mRecordButton.setText("Start Recordind");
		} else {
			try {
				initializeRecorder();
				mRecording = true;
				mRecorder.start();
				mRecordButton.setText("Stop Recordind");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void initializeRecorder() throws IllegalStateException, Exception {
		Log.d("INITRecorder", "STARTRecorder");
		mRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
		mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
		mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		mRecorder.setVideoFrameRate(1);
		mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
		mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		mRecorder.setOutputFile(this.makeFile().getAbsolutePath());
		mRecorder.prepare();
		Log.d("INITRecorder", "ENDRecorder");
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		/*try {
			mCamera.setDisplayOrientation(90);
			mCamera.setPreviewDisplay(holder);
			mCamera.startPreview();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	}
	
	/** A safe way to get an instance of the Camera object. */
	public static Camera getCameraInstance(){
	    Camera c = null;
	    try {
	        c = Camera.open(); // attempt to get a Camera instance
	    }
	    catch (Exception e){
	    	e.printStackTrace(); // Camera is not available (in use or does not exist)
	    }
	    return c; // returns null if camera is unavailable
	}
	
	private File makeFile() throws Exception 
	{
	    File dir = Environment.getExternalStorageDirectory();
	    File folder = new File(dir, "/wificam");
	    if(! folder.exists() && ! folder.mkdirs()) {
		    throw new Exception("Can't create directory");
	    }
	    return new File(folder, new SimpleDateFormat(
            "/'VIDEO_'yyyy'.'MM'.'dd'.'HH'.'mm'.'ss'.mp4'").format(new Date()));
	}
}
